package com.zijie.members.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.core.util.JsonResult;
import com.zijie.members.model.MembersAddress;
import com.zijie.members.model.search.MemberAddressSearchBean;
import com.zijie.members.service.MemberAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/memberAddress")
public class MembersAddressApi {
    private MemberAddressService memberAddressService;
    @Autowired
    public void setMemberAddressService(MemberAddressService memberAddressService) {
        this.memberAddressService = memberAddressService;
    }

    // 查询
    @GetMapping("/findAll")
    public ResponseEntity<JsonResult<MembersAddress>> findAll(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            MemberAddressSearchBean memberAddressSearchBean){
        Page<MembersAddress> page = Page.of(pageNo, pageSize);
        page = memberAddressService.findAll(page, memberAddressSearchBean);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    // 删除
    @DeleteMapping("/deleteById")
    public ResponseEntity<JsonResult<MembersAddress>> deleteById(@PathVariable Integer id) {
        int delete = memberAddressService.deleteById(id);
        return ResponseEntity.ok(JsonResult.success(delete));
    }

    // 添加
    @PostMapping("/add")
    public ResponseEntity<JsonResult<MembersAddress>> save(@RequestBody MembersAddress membersAddress) {
        boolean save = memberAddressService.save(membersAddress);
        return ResponseEntity.ok(JsonResult.success(save));
    }

    // 修改
    @PutMapping("/update")
    public ResponseEntity<JsonResult<MembersAddress>> update(@RequestBody MembersAddress membersAddress) {
        boolean update = memberAddressService.update(membersAddress);
        return ResponseEntity.ok(JsonResult.success(update));
    }

    // 批量删除
    @DeleteMapping("/deleteAll")
    public ResponseEntity<JsonResult<MembersAddress>> deleteAll(@RequestParam("ids") List<Integer> ids) {
        int delete = memberAddressService.deleteAll(ids);
        return ResponseEntity.ok(JsonResult.success(delete));
    }


    //根据会员id查询
    @GetMapping("/findByMemberId")
    public ResponseEntity<JsonResult<MembersAddress>> findByMemberId(@RequestParam Integer memberId) {
        MembersAddress membersAddress = memberAddressService.findByMemberId(memberId);
        return ResponseEntity.ok(JsonResult.success(membersAddress));
    }
}
